package com.mopa.renren;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewActivity extends Activity{
	public  static final String AUTH_BROADCAST = "com.mopa.oauth";
	private static final String mCallback=RenRenItem.CALLBACK;
	private WebView 	mWebView;
	private String  	mAuthResult=null;
	private Handler  	mHandler;
	private ProgressDialog dialog;
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Uri uri = getIntent().getData();
		if (uri != null)
		{
			mWebView = new WebView(this);
			mWebView.clearCache(true);
			mWebView.getSettings().setJavaScriptEnabled(true);
			mWebView.getSettings().setSupportZoom(true);
			mWebView.getSettings().setBuiltInZoomControls(true);
			mWebView.setWebViewClient(new MWebView());
			mWebView.setWebChromeClient(new MChromeClient());
			setContentView(mWebView);
			dialog = new ProgressDialog(this);
			dialog.setMessage("加载中..");
			mWebView.loadUrl(uri.toString());
		}
		else
		{
     		sendBroadcastResult();
			finish();
		}
	}
	public WebViewActivity()
	{
		mHandler = new Handler() {
			public void handleMessage(Message msg)
			{
				super.handleMessage(msg);
				switch (msg.what)
				{
				case 0:
					dialog.dismiss();
					break;
				default:
					dialog.show();
					break;
				}
			}
		};
	}
	private class MChromeClient extends WebChromeClient
	{
		private MChromeClient()
		{
			super();
		}
		public void onProgressChanged(WebView view, int newProgress)
		{
			super.onProgressChanged(view, newProgress);
		}

		public void onReceivedTitle(WebView view, String title)
		{
			super.onReceivedTitle(view, title);
		}
	}
	private class MWebView extends WebViewClient
	{
		public MWebView() {
			// TODO Auto-generated constructor stub
		}
		public boolean shouldOverrideUrlLoading(WebView view, String url)
		{
			Log.e("debug", "url:"+url);
			if (url.startsWith((new StringBuilder(String.valueOf(mCallback))).append("#").toString()))
			{
				mHandler.sendEmptyMessage(0);
				mAuthResult=url;
				returnResult();
				return false;
			}
			else
			{
				mHandler.sendEmptyMessage(1);
				view.loadUrl(url);
				return true;
			}
		}
		public void onPageStarted(WebView view, String url, Bitmap favicon)
		{
			mHandler.sendEmptyMessage(1);
		}

		public void onPageFinished(WebView view, String url)
		{
			mHandler.sendEmptyMessage(0);
		}

		public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
		{
			mHandler.sendEmptyMessage(0);
			super.onReceivedError(view, errorCode, description, failingUrl);
		}
	}
	
	
	/**
	 * 返回(1.发广播,2.返回)
	 */
	private void returnResult()
	{
		sendBroadcastResult();
		finish();
	}

	/**
	 * 发广播
	 */
	private void sendBroadcastResult()
	{
		Intent intent = new Intent(AUTH_BROADCAST);
		intent.putExtra("result", mAuthResult);
		sendBroadcast(intent);
	}
}
